<template>
  <div style="background-color: rgb(245, 247, 249);background: #fff;margin: 10px 10px 0px 10px;border-radius: 10px;">
    <div >
      <el-row class="app-toptool" type="flex" >
		  
		  
		  
		  
		 <el-col :span="8">
		   <div class="btn-group" style="margin-bottom:11px;float: right;">
		     <div>
		       <el-button
		         v-if="checkPermission('/miaoshagoods/add')"
		         type="primary"
		         size="small"
		         
		         @click="add()"
		       >添加</el-button>
		      
		     </div>
		   </div>
		 </el-col> 
		  
		  
		  
		  
        <el-col :span="16">
          <Search
            size="small"
            :search-visible="searchVisible"
            :search-data.sync="searchData"
            :search-form="searchForm"
            @refesh_list="searchgo"
          />
        </el-col>
		
		
       
		
		
		
      </el-row>
      <div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :row-class-name="rowClass"
          row-key="id"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          :border="false"
          :stripe="true"
          class="eltable"
          :data="list"
          style="width: 100%"
          @selection-change="selection"
        >
          <!-- <el-table-column align="center" type="selection" width="42" /> -->
          <el-table-column align="center" type="" property="id" label="编号" show-overflow-tooltip />
          <el-table-column align="center" property="image" label="图片" show-overflow-tooltip>
            <template slot-scope="scope">
              <div>
                <el-image v-if="scope.row.image" class="table_list_pic" :src="scope.row.image"
                  :preview-src-list="[scope.row.image]" />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            property="title"
            label="名称"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            property="begin_date"
            label="开始时间"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            property="end_date"
            label="结束时间"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            property="create_time"
            label="创建时间"
            show-overflow-tooltip
            width="125"
          />
          <el-table-column
            align="center"
            property="status"
            label="状态"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{scope.row.status==1 ? '正常' : '隐藏'}}
            </template>

          </el-table-column>
          <el-table-column
            align="center"
            property="sort"
            label="排序"
            show-overflow-tooltip
          >
            <template slot-scope="scope" class="sort">
              <el-input
                v-model="scope.row.sort"
                size="mini"
                placeholder="排序"
                @blur.stop="listUpdate(scope.row,'sort')"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.id">
                <el-button
                  v-if="checkPermission('/miaoshagoods/update')"
                  size="mini"
                  type="primary"
                  @click="update(scope.row)"
                >编辑</el-button>
                <el-button
                  v-if="checkPermission('/miaoshagoods/delete')"
                  type="danger"
                  size="mini"
                  @click="del(scope.row)"
                >删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="page_data.total"
          :page.sync="page_data.page"
          :limit.sync="page_data.limit"
          @pagination="index"
        />
      </div>
    </div>
    <!--添加/修改-->
    <Update :info="updateInfo" :opentype="opentype" :show.sync="dialog.updateDialogStatus" size="small" @refesh_list="index" />
  </div>
</template>
<script>
import Search from '@/components/common/Search'
import Pagination from '@/components/Pagination'
import Update from '@/views/miaoshagoods/update.vue'
import {
  confirm,
  param2Obj
} from '@/utils/common'
export default {
  name: 'miaoshagoods',
  components: {
    Search,
    Pagination,
    Update
  },
  data() {
    return {
      dialog: {
        updateDialogStatus: false
      },
      ids: [],
      single: true,
      multiple: true,
      list: [],
      opentype: 'add',
      updateInfo: {},
      loading: false,
      ws: {},
      filename: '',
      page_data: {
        limit: 20,
        page: 1,
        total: 20
      },
      searchVisible: true,
      searchForm: [],
      searchData: {}
    }
  },
  mounted() {
    this.index()
  },
  methods: {
    searchgo() {
      this.page_data.page = 1;
      this.index()
    },
    index() {
      const param = {
        limit: this.page_data.limit,
        page: this.page_data.page
      }
      Object.assign(param, this.searchData)
      Object.assign(param, param2Obj(this.$route.fullPath))
      this.loading = true

      this.$api.post('/miaoshagoods/index', param).then(res => {
        this.list = res.data.data
        this.page_data.total = res.data.total
        this.loading = false
        // console.log(this.page_data);
        if (this.page_data.page == 1) {
          this.searchForm = [
            {
              type: 'Input',
              label: '请输入查询内容',
              prop: 'keyword',
              width: '230px'
            }
          ]
        }
      })
    },
    listUpdate(row, field) {
      if (row.id) {
        this.$api.post('/miaoshagoods/listUpdate', {
          id: row.id,
          [field]: row[field]
        }).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        })
      }
    },
    add() {
      this.opentype = 'add'
      this.dialog.updateDialogStatus = true
    },
    update(row) {
      this.opentype = 'update'
      const id = row.id ? row.id : this.ids.join(',')
      this.$api.post('/miaoshagoods/getInfo', {
        id: id
      }).then(res => {
        this.dialog.updateDialogStatus = true
        this.updateInfo = res.data
      })
    },
    del(row) {
      confirm({
        content: '确定要操作吗'
      }).then(() => {
        const ids = row.id ? row.id : this.ids.join(',')

        this.$api.post('/miaoshagoods/delete', {
          id: ids
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.index()
        })
      }).catch(() => {})
    },
    selection(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    rowClass({
      row,
      rowIndex
    }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return 'rowLight'
        }
      }
    }
  }
}
</script>
<style scoped>
  .table_list_pic {
    width: 80px;
    vertical-align: middle;
  }
  
  .el-col-8 {
      width: 8%;
  }
  
  .el-col-16 {
      width: 75%;
      margin: 0px 0px 0px 70%;
  }
  
</style>
